package com.feng.zhihao.apigeteway.gateway.bean.impl.PARAMS_POSITION.REQ;

import java.io.IOException;
import java.io.InputStream;

import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.feng.zhihao.apigeteway.exception.GetewayException;
import com.feng.zhihao.apigeteway.gateway.ReqContext;
import com.feng.zhihao.apigeteway.gateway.bean.REQ_PARAMS_POSITION;
import com.feng.zhihao.apigeteway.gateway.bean.PARAMS_TYPE;
import com.feng.zhihao.apigeteway.util.BeanUtil;
import com.xiaoleilu.hutool.io.IoUtil;

@Component("REQ_PARAMS_POSITION_HTTP_BODY")
public class REQ_PARAMS_POSITION_HTTP_BODY implements REQ_PARAMS_POSITION {

	@Override
	public JSONObject getParams(ReqContext reqContext) {
		try (InputStream inputStream = reqContext.getRequest().getInputStream()){
			String reqBody=IoUtil.read(inputStream, "UTF-8");
			reqContext.setReqParamsString(reqBody);
			PARAMS_TYPE params_TYPE=(PARAMS_TYPE) BeanUtil.getBean(reqContext.getProjectInfo().getInterfaceParamsType());
			return params_TYPE.paresParams(reqBody, reqContext);
		} catch (IOException e) {
			throw new GetewayException(e,com.feng.zhihao.apigeteway.exception.ExceptionMessage.PARAMS_IO_EXCEPTION);
		}
		
	}

}
